<template>
  <div>
    <el-card>
      <el-row>
        <span>余志峰</span>
        <el-form label-width="80px" :model="queryInfo">
          <!-- 标签名称 -->
          <el-col :span="6">
            <el-form-item label="标签名称" prop="tags">
              <el-input
                placeholder="请输入"
                v-model="queryInfo.tags"
              ></el-input>
            </el-form-item>
          </el-col>
          <!-- 城市 -->
          <el-col :span="6">
            <el-form-item label="城市" prop="province">
              <el-select
                v-model="queryInfo.province"
                placeholder="请选择"
                @change="handleProvince"
              >
                <el-option
                  v-for="item in citySelect.province"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 地区 -->
          <el-col :span="6">
            <el-form-item label="地区" prop="city">
              <el-select v-model="queryInfo.city" placeholder="请选择">
                <el-option
                  v-for="item in citySelect.cityDate"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 企业简称-->
          <el-col :span="6">
            <el-form-item label="企业简称" prop="shortName">
              <el-input v-model="queryInfo.shortName"></el-input>
            </el-form-item>
          </el-col>
          <!-- 状态 -->
          <el-col :span="7">
            <el-form-item label="状态">
              <el-select v-model="statusData" placeholder="请选择">
                <el-option
                  v-for="item in status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <!-- 按钮 -->
            <el-button @click="clearClick">清除</el-button>
            <el-button type="primary" @click="gettableData(queryInfo)"
              >搜索</el-button
            >
          </el-col>
          <!-- 新增用户 -->
          <el-col :span="2">
            <el-button
              type="success"
              icon="el-icon-edit"
              @click="addClick"
              @close="closeAddDialog"
              >新增用户</el-button
            >
          </el-col>
        </el-form>
      </el-row>
      <!-- 提示标签语 -->
      <el-alert :title="countext" type="info" show-icon> </el-alert>
      <!-- table 页面 -->
      <el-table :data="tableData" style="width: 100%" class="table">
        <el-table-column prop="id" label="序号" width="120px">
        </el-table-column>
        <el-table-column prop="number" label="企业编号" width="140px">
        </el-table-column>
        <el-table-column prop="shortName" label="企业简称"> </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="creatorID" label="创业者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> </el-table-column>
        <el-table-column prop="remarks" label="备注"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{ row }">{{
            row.state === 1 ? '禁用' : '启用'
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template v-slot="{ row }">
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="edit(row)"
            ></el-button>
            <!-- 禁用 -->
            <el-tooltip
              class="item"
              effect="dark"
              :content="row.state === 1 ? '启用' : '禁用'"
              placement="top"
            >
              <el-button
                type="warning"
                icon="el-icon-close"
                circle
                plain
                @click="editzhuang(row)"
                v-if="row.state === 1"
              ></el-button>
              <el-button
                type="success"
                icon="el-icon-check"
                circle
                plain
                @click="editzhuang(row)"
                v-else
              ></el-button>
            </el-tooltip>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="deleteClick(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(page)"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="Number(pagesize)"
        layout="  prev, pager, next,sizes,jumper"
        :total="total"
        class="flooter"
      >
      </el-pagination>
    </el-card>
    <!-- 创建用户 -->
    <CompanysAdd
      ref="CompanyRef"
      :formBase="companysList"
      @newDataes="addComp"
      @handleCloseModal="dialogFormH"
      @handleAdd="addComp"
    ></CompanysAdd>
  </div>
</template>

<script>
import moment from 'moment'
import { list, remove } from '../../api/hmmm/companys'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { status } from '@/api/hmmm/constants'
import CompanysAdd from '../../module-hmmm/components/companys-add.vue'
export default {
  data () {
    return {
      companysList: {},
      countext: '',
      total: 0,
      citySelect: {
        province: [],
        cityDate: []
      },
      provinces,
      citys,
      status,
      statusData: '',
      tableData: [],
      queryInfo: {
        shortName: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        page: 1,
        pagesize: 10
      },
      page: '',
      pagesize: ''
    }
  },

  created () {
    this.getDatacity()
    this.gettableData()

    console.log(provinces())
  },

  methods: {
    // 刷新数据
    async gettableData (queryInfo) {
      const res = await list(queryInfo)
      console.log(res)
      this.tableData = res.data.items
      this.counts = res.data.counts
      // 警告字
      this.countext = `共${this.counts}数据`
      // 时间格式话
      res.data.items.forEach(item => {
        item.addDate = moment(item.addDate).format('YYYY-MM-DD')
      })
    },
    closeAddDialog () {
      this.$refs.CompanyRef.resetFields()
      this.addForm.videoURL = ''
    },
    // 弹出框取消事件
    dialogFormH () {
      this.$refs.add.dialogFormH()
    },
    // 底部分页
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.gettableData()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.gettableData()
    },
    // 清除按钮
    clearClick () {
      this.queryInfo.tags = ''
      this.queryInfo.province = ''
      this.queryInfo.city = ''
      this.queryInfo.shortName = ''
      this.statusData = ''
      this.gettableData()
      return this.$message.success('已清除所有内容！')
    },
    // 添加省份

    getDatacity () {
      this.citySelect.province = provinces()
      console.log(provinces())
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
      this.queryInfo.city = this.citySelect.cityDate[0]
    },
    // 点击添加
    addClick () {
      this.companysList = {}
      this.$refs.CompanyRef.dialogFormV()
    },
    addComp () {
      this.gettableData()
    },
    // 编辑按钮
    edit (row) {
      this.companysList = row
      console.log(this.companysList)
      this.$refs.CompanyRef.dialogFormV()
    },
    // 禁用按钮
    editzhuang (row) {
      console.log(row)
      if (row.state === 1) {
        row.state = 0
      } else if (row.state === 0) {
        row.state = 1
      }
    },
    // 删除按钮
    async deleteClick (row) {
      const configDelete = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (configDelete !== 'confirm') {
        return this.$message.info('您取消操作')
      }
      try {
        const { data } = await remove(row)
        console.log(data)
        this.gettableData()
        this.$message.success('删除成功')
      } catch (error) {
        this.$message.error('删除失败')
      }
    }
  },
  components: {
    CompanysAdd
  }
}
</script>

<style scoped lang="less">
.el-card {
  padding: 5px;
  margin: 20px;
}
.table {
  margin-top: 20px;
  text-align: center;
}
.flooter {
  margin-top: 20px;
}
</style>
